package com.cf.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日期处理
 * 
 */
public class DateUtils {

	private static final Logger log = LoggerFactory.getLogger(DateUtils.class);

	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}

	public static Date getDate(String sFormat, String sDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
		Date d = null;

		try {
			sdf.setLenient(false);
			d = sdf.parse(sDate);

		} catch (Exception e) {
			// e.printStackTrace();
		}
		return d;
	}

	public static Date getDate(String sDate) {
		String[] sa = { "yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd", "yyyy/MM/dd",
				"yyyy年MM月dd日" };
		for (String s : sa) {
			Date d = getDate(s, sDate);
			if (d == null) {
				continue;
			} else {
				return d;
				// new SimpleDateFormat(sa[0]).format(d);
			}
		}
		return null;
	}

}
